﻿using coremvcdemo.Code;
using coremvcdemo.Dao;
using coremvcdemo.Helper;
using static coremvcdemo.Controllers.LoginController;

namespace coremvcdemo.Services.Impl
{
    [Service]
    //[Service(ServiceLifetime.Scoped)]
    public class LoginService : ILoginService
    {
        private readonly otherContext dbcontext;
        private readonly JsEncryptHelper encryptHelper;

        public LoginService(otherContext dbcontext, JsEncryptHelper encryptHelper)
        {
            this.dbcontext = dbcontext;
            this.encryptHelper = encryptHelper;
        }

        public Dictionary<string, object> getlogin(loginModel model, ISession session)
        {
            model.password = encryptHelper.Decrypt(model.password);
            var jg = dbcontext.DB_VB_教工信息.SingleOrDefault(j => j.卡号 == model.username && j.密码 == model.password);
            if (jg == null)
            {
                return MyUtils.getFailDic("登陆失败,请校验卡号密码");
            }
            session.SetString("code", jg.卡号);
            return MyUtils.getSuccessDic("index.html");
        }

        public Dictionary<string, object> getUserMsg(string Account)
        {
            var jg = dbcontext.DB_VB_教工信息.SingleOrDefault(j => j.卡号 == Account);
            var user = new
            {
                id = jg.ID,
                userName = jg.姓名,
                cardNum = jg.卡号,
                departmentName = jg.部门
            };
            return MyUtils.getSuccessDic(null, user);
        }
    }
}
